package cn.ibizlab.odoo.core.odoo_im_livechat.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iim_livechat_channel;
import cn.ibizlab.odoo.core.odoo_im_livechat.domain.Im_livechat_channel;
import cn.ibizlab.odoo.core.odoo_im_livechat.filter.Im_livechat_channelSearchContext;
import cn.ibizlab.odoo.core.odoo_im_livechat.service.IIm_livechat_channelService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[即时聊天] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Im_livechat_channelServiceImpl implements IIm_livechat_channelService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iim_livechat_channelClientService im_livechat_channelClientService;

    @Override
    public Im_livechat_channel get(Integer id) {
        Iim_livechat_channel clientModel = im_livechat_channelClientService.createModel();
        clientModel.setId(id);
        im_livechat_channelClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Im_livechat_channel get(Im_livechat_channel im_livechat_channel) {
        Im_livechat_channel retim_livechat_channel = this.get(im_livechat_channel.getId()) ;
    	BeanCopier copier=BeanCopier.create(retim_livechat_channel.getClass(), im_livechat_channel.getClass(), false);
        copier.copy(retim_livechat_channel, im_livechat_channel, null);
        return im_livechat_channel ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //im_livechat_channelClientService.removeBatch(im_livechat_channels) ;

    }

    @Override
    public boolean update(Im_livechat_channel im_livechat_channel) {
        boolean bOk = false ;
        Iim_livechat_channel clientModel = this.convert2Model(im_livechat_channel,null);
        im_livechat_channelClientService.update(clientModel) ;
        this.convert2Domain(clientModel,im_livechat_channel) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iim_livechat_channel clientModel = im_livechat_channelClientService.createModel();
        clientModel.setId(id);
        im_livechat_channelClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void createBatch(List<Im_livechat_channel> im_livechat_channels, int batchSize) {

                        //im_livechat_channelClientService.createBatch(im_livechat_channels,batchSize) ;

    }

    @Override
    public void updateBatch(List<Im_livechat_channel> im_livechat_channels, int batchSize) {

        //im_livechat_channelClientService.updateBatch(im_livechat_channels,batchSize) ;

    }

    @Override
    public boolean create(Im_livechat_channel im_livechat_channel) {
        boolean bOk = false ;
        Iim_livechat_channel clientModel = this.convert2Model(im_livechat_channel,null);
        im_livechat_channelClientService.create(clientModel) ;
        this.convert2Domain(clientModel,im_livechat_channel);
        bOk = true ;

        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Im_livechat_channel> searchDefault(Im_livechat_channelSearchContext context) {
        List<Im_livechat_channel> list = new ArrayList<Im_livechat_channel>();
        Page<cn.ibizlab.odoo.core.client.model.Iim_livechat_channel> clientModelList = im_livechat_channelClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iim_livechat_channel clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Im_livechat_channel>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Im_livechat_channel> selectByOdoo_create(Integer id){
        Im_livechat_channelSearchContext context = new Im_livechat_channelSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Im_livechat_channel> list = new ArrayList<Im_livechat_channel>();
        Page<cn.ibizlab.odoo.core.client.model.Iim_livechat_channel> clientModelList = im_livechat_channelClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iim_livechat_channel clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Im_livechat_channel>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Im_livechat_channel> selectByOdoo_write(Integer id){
        Im_livechat_channelSearchContext context = new Im_livechat_channelSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Im_livechat_channel> list = new ArrayList<Im_livechat_channel>();
        Page<cn.ibizlab.odoo.core.client.model.Iim_livechat_channel> clientModelList = im_livechat_channelClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iim_livechat_channel clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Im_livechat_channel>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iim_livechat_channel convert2Model(Im_livechat_channel domain , Iim_livechat_channel model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = im_livechat_channelClientService.createModel() ;
        }

         if(domain.getUser_idsDirtyFlag())
            model.setUser_ids(domain.getUser_ids());
         if(domain.getWebsite_urlDirtyFlag())
            model.setWebsite_url(domain.getWebsite_url());
         if(domain.getRule_idsDirtyFlag())
            model.setRule_ids(domain.getRule_ids());
         if(domain.getImageDirtyFlag())
            model.setImage(domain.getImage());
         if(domain.getWeb_pageDirtyFlag())
            model.setWeb_page(domain.getWeb_page());
         if(domain.getIs_publishedDirtyFlag())
            model.setIs_published(domain.getIs_published());
         if(domain.getScript_externalDirtyFlag())
            model.setScript_external(domain.getScript_external());
         if(domain.getNbr_channelDirtyFlag())
            model.setNbr_channel(domain.getNbr_channel());
         if(domain.getButton_textDirtyFlag())
            model.setButton_text(domain.getButton_text());
         if(domain.getWebsite_descriptionDirtyFlag())
            model.setWebsite_description(domain.getWebsite_description());
         if(domain.getChannel_idsDirtyFlag())
            model.setChannel_ids(domain.getChannel_ids());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getWebsite_publishedDirtyFlag())
            model.setWebsite_published(domain.getWebsite_published());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getImage_mediumDirtyFlag())
            model.setImage_medium(domain.getImage_medium());
         if(domain.getRating_percentage_satisfactionDirtyFlag())
            model.setRating_percentage_satisfaction(domain.getRating_percentage_satisfaction());
         if(domain.getInput_placeholderDirtyFlag())
            model.setInput_placeholder(domain.getInput_placeholder());
         if(domain.getImage_smallDirtyFlag())
            model.setImage_small(domain.getImage_small());
         if(domain.getAre_you_insideDirtyFlag())
            model.setAre_you_inside(domain.getAre_you_inside());
         if(domain.getDefault_messageDirtyFlag())
            model.setDefault_message(domain.getDefault_message());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
        return model ;
    }

    public Im_livechat_channel convert2Domain( Iim_livechat_channel model ,Im_livechat_channel domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Im_livechat_channel();
        }

        if(model.getUser_idsDirtyFlag())
            domain.setUser_ids(model.getUser_ids());
        if(model.getWebsite_urlDirtyFlag())
            domain.setWebsite_url(model.getWebsite_url());
        if(model.getRule_idsDirtyFlag())
            domain.setRule_ids(model.getRule_ids());
        if(model.getImageDirtyFlag())
            domain.setImage(model.getImage());
        if(model.getWeb_pageDirtyFlag())
            domain.setWeb_page(model.getWeb_page());
        if(model.getIs_publishedDirtyFlag())
            domain.setIs_published(model.getIs_published());
        if(model.getScript_externalDirtyFlag())
            domain.setScript_external(model.getScript_external());
        if(model.getNbr_channelDirtyFlag())
            domain.setNbr_channel(model.getNbr_channel());
        if(model.getButton_textDirtyFlag())
            domain.setButton_text(model.getButton_text());
        if(model.getWebsite_descriptionDirtyFlag())
            domain.setWebsite_description(model.getWebsite_description());
        if(model.getChannel_idsDirtyFlag())
            domain.setChannel_ids(model.getChannel_ids());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getWebsite_publishedDirtyFlag())
            domain.setWebsite_published(model.getWebsite_published());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getImage_mediumDirtyFlag())
            domain.setImage_medium(model.getImage_medium());
        if(model.getRating_percentage_satisfactionDirtyFlag())
            domain.setRating_percentage_satisfaction(model.getRating_percentage_satisfaction());
        if(model.getInput_placeholderDirtyFlag())
            domain.setInput_placeholder(model.getInput_placeholder());
        if(model.getImage_smallDirtyFlag())
            domain.setImage_small(model.getImage_small());
        if(model.getAre_you_insideDirtyFlag())
            domain.setAre_you_inside(model.getAre_you_inside());
        if(model.getDefault_messageDirtyFlag())
            domain.setDefault_message(model.getDefault_message());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        return domain ;
    }


}
